package com.y2w.gulimall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.y2w.gulimall.order.entity.OrderEntity;
import com.y2w.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.y2w.common.utils.PageUtils;
import com.y2w.common.utils.Query;

import com.y2w.gulimall.order.dao.OrderItemDao;
import com.y2w.gulimall.order.entity.OrderItemEntity;
import com.y2w.gulimall.order.service.OrderItemService;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity content, Channel channel){
        byte[] body = message.getBody();
        long tag = message.getMessageProperties().getDeliveryTag();
        try {
            if(tag%2==0){
                channel.basicAck(tag, false);
                System.out.println("签收了货物。。、" + tag);
            }else{
                //long deliveryTag, boolean multiple, boolean requeue
                channel.basicNack(tag,false,true);
                //long deliveryTag, boolean requeue
                //channel.basicReject();
                System.out.println("没签收货物。。、" + tag);
            }
        } catch (IOException e) {
        //    网络中断
        }
        System.out.println("接收消息。。。内容"+message+"==>内容："+content);
    }
    @RabbitHandler
    public void recieveMessage(OrderEntity entity){

        System.out.println("接收消息。。。==>内容："+entity);
    }

}
